package com.xiaoniu.forum.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xiaoniu.forum.common.utils.RemoveIdsUtils;
import com.xiaoniu.forum.entity.vo.CommunityTopicOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiaoniu.forum.entity.CommunityEntity;
import com.xiaoniu.forum.service.CommunityService;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.R;



/**
 * 社区分类的模块表, 比如编程分类有JAVA模块 Mysql模块 
 *
 * @author xiaoniu
 * @email sunlightcs@gmail.com
 * @date 2021-03-16 15:11:51
 */
@RestController
@RequestMapping("forum/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    /**
     * 列表,也可以带条件
     */
    @RequestMapping("/list")
    //@RequiresPermissions("forum:community:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = communityService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 查询所有
     */
    @RequestMapping("/all")
    public R all(){
        List<CommunityEntity> list = communityService.list();
        return R.ok().put("data",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("forum:community:info")
    public R info(@PathVariable("id") String id){
		CommunityEntity community = communityService.getById(id);

        return R.ok().put("community", community);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("forum:community:save")
    public R save(@RequestBody CommunityEntity community){
		communityService.save(community);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("forum:community:update")
    public R update(@RequestBody CommunityEntity community){
		communityService.updateById(community);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("forum:community:delete")
    public R delete(@RequestBody String[] ids){
        int preLen = ids.length;

        Integer failCount = communityService.removeByIds(Arrays.asList(ids));

        return RemoveIdsUtils.returnMsg(preLen,failCount);
    }

    ///**
    // * 根据id查询社区名称
    // * @param communityId
    // * @return 返回社区分类名称 String类型
    // */
    //@RequestMapping("community/{id}")
    //public R getByCommunityCategoryById(@PathVariable String id){
    //    String communityName = communityService.getCommunityNameById(id);
    //    return R.ok().put("communityName",communityName);
    //}

    @GetMapping("/option/{communityCategoryId}")
    public R getAllWithOptionsBycommunityCategoryId(@PathVariable String communityCategoryId){
        List<CommunityTopicOptionVo> list =communityService.getAllWithOptionsBycommunityCategoryId(communityCategoryId);
        return R.ok().put("data",list);
    }



}
